package cn.com.w.sharding.service.impl;

import cn.com.w.sharding.dao.UserInfoMapper;
import cn.com.w.sharding.domain.UserInfo;
import cn.com.w.sharding.service.IUserInfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangxiaolong
 * @since 2022-06-22
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {


    @ShardingTransactionType(TransactionType.XA)
    @Transactional
    @Override
    public String getOne(String userNo) {
        UserInfo one = this.getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserNo, userNo).last("limit 1"));
        if (Objects.isNull(one)) {
            return "error";
        }
        LambdaUpdateWrapper<UserInfo> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(UserInfo::getUserAge, 66);
        updateWrapper.eq(UserInfo::getUserNo, one.getUserNo());
        this.update(updateWrapper);

        int i = 1/0;

        UserInfo one1 = this.getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserNo, userNo).last("limit 1"));
        return JSONObject.toJSONString(one1);
    }
}
